package org.jeecg.modules.assets.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;

import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;

import org.jeecg.modules.assets.dto.AssetsItemDTO;
import org.jeecg.modules.assets.dto.AssetsPageDTO;
import org.jeecg.modules.assets.dto.CompanyDTO;
import org.jeecg.modules.assets.entity.AssetsCategory;
import org.jeecg.modules.assets.entity.AssetsDoubleQuotationResult;
import org.jeecg.modules.assets.entity.AssetsDoubleQuotationResultItem;
import org.jeecg.modules.assets.entity.AssetsItem;
import org.jeecg.modules.assets.service.IAssetsCategoryService;
import org.jeecg.modules.assets.service.IAssetsDoubleQuotationResultItemService;
import org.jeecg.modules.assets.service.IAssetsDoubleQuotationResultService;
import org.jeecg.modules.assets.service.IAssetsItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 资产明细
 * @Author: jeecg-boot
 * @Date: 2023-12-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "移动端资产明细")
@RestController
@RequestMapping("/app/assets/assetsItem")
public class AppAssetsItemController extends JeecgController<AssetsItem, IAssetsItemService> {
    @Autowired
    private IAssetsItemService assetsItemService;
    @Autowired
    private IAssetsDoubleQuotationResultService assetsDoubleQuotationResultService;
    @Autowired
    private IAssetsDoubleQuotationResultItemService iAssetsDoubleQuotationResultItemService;
    @Autowired
    private IAssetsCategoryService assetsCategoryService;

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "资产明细-分页列表查询")
    @ApiOperation(value = "资产明细-分页列表查询", notes = "资产明细-分页列表查询")
    @GetMapping(value = "/getAssetsListByUser")
    public Result<?> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "queryType", required = false, defaultValue = "1") String queryType,
            @RequestParam(name = "likeValue", required = false) String likeValue,
            @RequestParam(name = "owner", required = false) String owner
    ) {
        //根据当前登录用户查询用户资产
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        AssetsPageDTO result = new AssetsPageDTO();
        IPage<AssetsItem> pageList = null;
        Page<AssetsItem> page = new Page<AssetsItem>(pageNo, pageSize);
        QueryWrapper<AssetsItem> queryWrapper = new QueryWrapper<>();
        if (queryType.equals("1")) {
            QueryWrapper<AssetsItem> queryCount = new QueryWrapper<>();
            if (user.getDepartIds().startsWith("1200")) {
                queryWrapper.eq("dept", user.getDepartIds()).and(qwItem -> {
                    qwItem.eq("owner", user.getRealname()).or().eq("userer", user.getRealname());
                });
                queryCount.eq("dept", user.getDepartIds());
                queryCount.eq("owner", user.getRealname());

            } else {
                queryWrapper.eq("owner", user.getRealname());
                queryWrapper.or().eq("userer", user.getRealname());
                queryCount.eq("owner", user.getRealname());
            }
            long count = assetsItemService.count(queryCount);
            result.setAssetsNum(count);
            pageList = assetsItemService.page(page, queryWrapper);
        } else {
            queryWrapper.eq("dept", user.getDepartIds());
            if (StringUtils.isNotBlank(owner)) {
                queryWrapper.and(qwItem -> {
                    qwItem.like("asset_coding", owner).or();
                    qwItem.like("asset_name", owner).or();
                    qwItem.like("owner", owner);
                });
            }
            pageList = assetsItemService.page(page, queryWrapper);
            result.setAssetsNum(pageList.getTotal());

        }
        result.setTotal(pageList.getTotal());
        result.setRecords(pageList.getRecords());

        if (CollectionUtils.isNotEmpty(pageList.getRecords())) {
            result.setCreateTime(pageList.getRecords().get(0).getCreateTime());
        }

        return Result.OK(result);
    }


    /**
     * 分页列表查询
     *
     * @param assetsItem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "资产明细-获取抽盘待选明细")
    @ApiOperation(value = "资产明细-获取抽盘待选明细", notes = "资产明细-获取抽盘待选明细")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AssetsItem assetsItem,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        Page<AssetsItemDTO> page = new Page<AssetsItemDTO>(pageNo, pageSize);
        IPage<AssetsItemDTO> pageList = assetsItemService.queryPageListBydept(page, assetsItem.getDept(), assetsItem.getCompany(), assetsItem.getAssetName());
        for (AssetsItem record : pageList.getRecords()) {
            //判断分类
            if (record.getCategory() != null) {
                AssetsCategory byId = assetsCategoryService.getById(record.getCategory());
                if (byId != null) {
                    assetsItem.setAssetCoding(byId.getAlias() != null ? byId.getAlias() + assetsItem.getAssetCoding() : assetsItem.getAssetCoding());
                }
            }
        }

        return Result.OK(pageList);
    }

    //获取用户对应的资产公司
    @AutoLog(value = "资产明细-获取用户对应的资产公司")
    @ApiOperation(value = "资产明细-获取用户对应的资产公司", notes = "资产明细-获取用户对应的资产公司")
    @GetMapping(value = "/getCompanyByUser")
    public Result<?> getCompanyByUser() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<CompanyDTO> dtoList = assetsItemService.getCompanyByUser(user.getRealname());
        return Result.OK(dtoList);
    }


    /**
     * 添加
     *
     * @param assetsItem
     * @return
     */
    @AutoLog(value = "资产明细-添加")
    @ApiOperation(value = "资产明细-添加", notes = "资产明细-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AssetsItem assetsItem) {
        assetsItemService.save(assetsItem);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param assetsItem
     * @return
     */
    @AutoLog(value = "资产明细-编辑")
    @ApiOperation(value = "资产明细-编辑", notes = "资产明细-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AssetsItem assetsItem) {
        assetsItemService.editItem(assetsItem);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资产明细-通过id删除")
    @ApiOperation(value = "资产明细-通过id删除", notes = "资产明细-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        assetsItemService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "资产明细-批量删除")
    @ApiOperation(value = "资产明细-批量删除", notes = "资产明细-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.assetsItemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资产明细-通过id查询")
    @ApiOperation(value = "资产明细-通过id查询", notes = "资产明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AssetsItem assetsItem = assetsItemService.getById(id);
        return Result.OK(assetsItem);
    }


    /**
     * 通过id查询
     *
     * @param
     * @return
     */
    @AutoLog(value = "资产明细-通过code查询")
    @ApiOperation(value = "资产明细-通过code查询", notes = "资产明细-通过code查询")
    @GetMapping(value = "/queryByCodeing")
    public Result<?> queryByCodeing(@RequestParam(name = "codeing", required = true) String codeing, @RequestParam(name = "company", required = true) String company) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("asset_coding", codeing.substring(codeing.length() - 6));
        qw.eq("company", company);
        AssetsItem assetsItem = assetsItemService.getOne(qw);
        if (assetsItem != null) {
            return Result.OK(assetsItem);
        } else {
            qw = new QueryWrapper();
            qw.eq("asset_name", codeing);
            return Result.OK(assetsItem);
        }

    }


    /**
     * 导出excel
     *
     * @param request
     * @param assetsItem
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AssetsItem assetsItem) {
        return super.exportXls(request, assetsItem, AssetsItem.class, "资产明细");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AssetsItem.class);
    }

}
